package com.zhijiaoyun.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.zhijiaoyun.controller.base.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.common.FileUtils;
import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.log.SystemLog;
import com.zhijiaoyun.model.News;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Controller
@RequestMapping(value = "news", produces = "application/json; charset=utf-8")
public class NewsController extends BaseController {

    /**
     * 新增资讯
     *
     * @param title
     * @param summary
     * @param keyWord
     * @param source
     * @param author
     * @param content
     * @param imageUrl
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addNews", method = RequestMethod.POST)
    @SystemLog(function = "addNews", description = "添加资讯")
    public Map<String, Object> addNews(String title, String summary, String keyWord, String source, String author,
                                       String content, @RequestParam("imageUrl") MultipartFile imageUrl, HttpServletRequest request) {
        if (StringUtils.isEmptyString(title)) {
            return responseConditionError();
        }
        News news = newsService.getNewsByTitle(title);
        if (news != null) {
            return responseErrorMap(DsConstant.CODE_NEWS_LOST, DsConstant.MESSAGE_NEWS_LOST);
        }
        String pictureStr = FileUtils.saveNewsPicture(imageUrl);
        if ("".equals(pictureStr)) {
            return responseFileUploadError();
        }
        return newsService.insert(title, summary, keyWord, pictureStr, source, author, content, request);
    }

    /**
     * 修改资讯
     *
     * @param newsId
     * @param title
     * @param summary
     * @param keyWord
     * @param source
     * @param author
     * @param content
     * @param imageUrl
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{newsId}/modifyNews", method = RequestMethod.POST)
    @SystemLog(function = "modifyNews", description = "修改资讯信息")
    public Map<String, Object> modifyNews(@PathVariable("newsId") Long newsId, String title, String summary,
                                          String keyWord, String source, String author, String content,
                                          @RequestParam(value = "imageUrl", required = false) MultipartFile imageUrl, HttpServletRequest request) {
        News news = newsService.getNewsByNewsId(newsId);
        if (news == null) {
            return responseErrorMap(DsConstant.CODE_NEWS_LOST, DsConstant.MESSAGE_NEWS_LOST);
        }
        if (StringUtils.isEmptyString(title)) {
            return responseConditionError();
        } else {
            News temp = newsService.getNewsByTitleNotNewsId(newsId, title);
            if (temp != null) {
                return responseErrorMap(DsConstant.CODE_NEWS_NAME_EXIST, DsConstant.MESSAGE_NEWS_NAME_EXIST);
            }
        }
        String pictureStr = FileUtils.saveNewsPicture(imageUrl);
        return newsService.modify(news, title, keyWord, pictureStr, source, author, summary, content, request);
    }

    /**
     * 删除资讯
     *
     * @param newsId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{newsId}/deleteNews", method = RequestMethod.POST)
    @SystemLog(function = "deleteNews", description = "删除资讯")
    public Map<String, Object> deleteNews(@PathVariable("newsId") long newsId) {
        return newsService.deleteNewsById(newsId);
    }

    /**
     * 资讯列表
     *
     * @param pageIndex
     * @param pageSize
     * @param title
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/newsList", method = RequestMethod.GET)
    public Map<String, Object> newsList(int pageIndex, int pageSize, String title) {
        if (pageIndex <= 0) {
            pageIndex = 1;
        }
        if (pageSize <= 0) {
            pageSize = DsConstant.PAGESIZE_DEFAULT;
        }
        return newsService.pageNews(pageIndex, pageSize, title);
    }

    /**
     * 资讯详情
     *
     * @param newsId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{newsId}/newsDetail", method = RequestMethod.GET)
    public Map<String, Object> newsDetail(@PathVariable("newsId") long newsId) {
        return newsService.getNewsById(newsId);
    }

    /**
     * 修改推荐资讯
     *
     * @param newsId
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{newsId}/changeRecommend", method = RequestMethod.POST)
    @SystemLog(function = "changeRecommend", description = "修改资讯推荐")
    public Map<String, Object> changeRecommend(@PathVariable("newsId") long newsId, HttpServletRequest request) {
        return newsRecommendService.updateRecommend(newsId, request);
    }

    /**
     * 推荐列表
     *
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/newsRecommendList", method = RequestMethod.GET)
    public Map<String, Object> newsRecommendList(int pageIndex, int pageSize) {
        if (pageIndex <= 0) {
            pageIndex = 1;
        }
        if (pageSize <= 0) {
            pageSize = DsConstant.PAGESIZE_DEFAULT;
        }
        return newsRecommendService.page(pageIndex, pageSize);
    }

}
